
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .tab{
            width: 500px;
            min-height: 300px;
            border: 1px solid;
            margin: 50px auto;
        }
        .tab .btn{
            height: 35px;
            background-color: orange;
            display: flex;
            text-align: center;
            line-height: 35px;
        }
        .tab .btn span{
            width: 20%;
            cursor: pointer;
        }
        .tab .btn span.bg{
            background-color: greenyellow;
        }
        .tab ul{
            height: 265px;
            background-color: hotpink;
        }
        .tab ul li{
            height: 265px;
            list-style: none;
            text-align: center;
            line-height: 265px;
            display: none;
            font-size: 30px;
        }
        .tab ul li.show{
            display: block;
        }
    </style>
</head>
<body>
    <div class="tab">
        <div class="btn">
            <span class="bg">新闻</span>
            <span>体育</span>
            <span>娱乐</span>
            <span>军事</span>
            <span>游戏</span>
        </div>
        <ul>
            <li class="show">新闻</li>
            <li>体育</li>
            <li>娱乐</li>
            <li>军事</li>
            <li>游戏</li>
        </ul>
    </div>
    <script>
        // 面向对象的原则：需要把所有的东西都变成对象的属性和方法
        class Tab{
            constructor(){
            this.btn = document.querySelectorAll('.tab .btn span')
            this.li = document.querySelectorAll('.tab ul li')
            this.init()
            }
            init(){
                this.addEvent()
            }
           
            addEvent(){
                for(let i=0;i<this.btn.length;i++){
                    this.btn[i].onclick = ()=>{
                    this.clearStyle()
                    this.btn[i].classList.add('bg')
                    this.li[i].classList.add('show')
                    }
                }
            }
            clearStyle(){
                for(let i=0;i<this.btn.length;i++){
                    this.btn[i].className=''
                    this.li[i].className=''
                }
            }
        }
        new Tab()
    </script>
</body>
</html>